package com.example.onlinemonitoring.comCenter;

import java.text.ParseException;
import java.util.Date;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Ejhistory;
import com.example.onlinemonitoring.model.Ejsensor;
import com.example.onlinemonitoring.serviceImpl.EjsensorServiceImpl;

import lombok.Data;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.Thread;
import java.util.List;
import java.util.ArrayList;

/**
 * 母线变形
 */
@Service("ejCom")
@Scope("prototype")
@Data
public class EjCom extends CommPortManager {
    private static int currcmd = 0;
    int recvflag = 1;
    private static int pointcount = 1;
    private static int addrint = 1;
    private static final int CMD1 = 1;// 广播
    private static final int CMD2 = 2;//
    private static final int CMD3 = 3;
    private static final int CMD4 = 4;//
    private static final int CMD5 = 5;
    private static final int CMD6 = 6;//
    private static final int CMD7 = 7;
    private static final int CMD8 = 8;//
    private static final int CMD9 = 9;
    private static final int CMD10 = 10;//
    int alarmLevel = 0;// 报警等级
    String alarmMessage = "";// 报警消息
    List<Integer> array = new ArrayList<>();
    @Autowired
    private EjsensorServiceImpl Ejserviceimpl;
    // 锁对象
    Lock lck = new ReentrantLock();

    @Override
    public void receiveData(String str) {
        try {

            // lck.lock();
            receiveDatas(str);
            // Thread.sleep((int) Math.round(ad.getTimersp() * 1000));
            // lock.notifyAll();
            // lck.unlock();

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void receiveDatas(String str) {
        str = str.toUpperCase();
        setText(str);
        if (str.length() < 14) {
            return;
        }

        String addrstr = str.substring(0, 2);
        addrint = parseHex4(addrstr);
        // Integer addrint = 1;
        reSend[addrindex] = 0;
        String cmdstr = str.substring(2, 4);
        String datalenstr = str.substring(4, 6);
        Integer datalen = parseHex4(datalenstr);
        Integer cmdint = parseHex4(cmdstr);
        byte[] carr = HexUtil.hexString2Bytes(str);
        Ejsensor ej = new Ejsensor();
        ej.setStationid(ad.getStationid());
        ej.setDevicegroup(ad.getDevicegroup());
        ej.setDeviceaddr(addrindex);
        ej.setPointaddr(1);
        Ejsensor record = Ejserviceimpl.getModel(ej);
        if (record == null) {
            setText("当前数据库无监测点：设备地址" + addrint + "的监测点" + " ！ 请先添加该监测点！");
            return;
        }
        ej.setLastdatatime(new Date());

        int startindex = 6;
        switch (cmdint) {
            case 2:
                ej.setLastdevicefaultsignal(carr[3] >> 0 & 1);
                ej.setLasttimingsignalstatus(carr[3] >> 1 & 1);
                ej.setLasttimeservicestatus(carr[3] >> 2 & 1);
                ej.setLasttimehoppingframemeasurementstatus(carr[3] >> 3 & 1);
                ej.setLastinput1(carr[3] >> 4 & 1);
                ej.setLastinput2(carr[3] >> 5 & 1);
                ej.setLastinput3(carr[3] >> 6 & 1);
                ej.setLastinput4(carr[3] >> 7 & 1);

                ej.setLastinput5(carr[4] >> 0 & 1);
                ej.setLastinput6(carr[4] >> 1 & 1);
                ej.setLastinput7(carr[4] >> 2 & 1);
                ej.setLastinput8(carr[4] >> 3 & 1);
                ej.setLastoutlet1status(carr[4] >> 4 & 1);
                ej.setLastoutlet2status(carr[4] >> 5 & 1);
                ej.setLastoutlet3status(carr[4] >> 6 & 1);
                ej.setLastoutlet4status(carr[4] >> 7 & 1);

                ej.setLastoutlet5status(carr[5] >> 0 & 1);
                ej.setLastoutlet6status(carr[5] >> 1 & 1);
                ej.setLastoutlet7status(carr[5] >> 2 & 1);
                ej.setLastoutlet8status(carr[5] >> 3 & 1);
                ej.setLastalarmstatus(carr[5] >> 4 & 1);
                ej.setLastdeploymentstatus(carr[5] >> 5 & 1);
                ej.setLastdisplacementalarm(carr[5] >> 6 & 1);
                ej.setLasttemperaturealarm(carr[5] >> 7 & 1);

                ej.setLasthumidityalarm(carr[6] >> 0 & 1);
                String alm = "";
                ej.setLastalarmlevel(0);
                ej.setLastalarmmessage("");
                if (ej.getLastdisplacementalarm() == 1) {
                    alm += "位移告警,";
                    ej.setLastalarmlevel(2);
                    ej.setLastalarmmessage(alm);
                }
                if (ej.getLasttemperaturealarm() == 1) {
                    alm += "温度告警,";
                    ej.setLastalarmlevel(2);
                    ej.setLastalarmmessage(alm);
                }
                if (ej.getLasthumidityalarm() == 1) {
                    alm += "湿度告警,";
                    ej.setLastalarmlevel(2);
                    ej.setLastalarmmessage(alm);
                }
                if (alm.length() == 0) {
                    GetNormalAlarm("伸缩节" + ej.getDeviceaddr(), record.getDevicegroup(), ej.getDeviceaddr(), 0,
                            ej.getPointname(), "", false);
                    setText("无报警");
                } else {
                    GetNormalAlarm("伸缩节" + ej.getDeviceaddr(), record.getDevicegroup(), ej.getDeviceaddr(), 0,
                            ej.getPointname(), alm, true);
                    setText(alm);
                }
                break;

            case 3:
                setText(ad.getName() + "设备返回数据： 站点号：1 地址1:" + addrint);

                if (addrint == 1) {
                    if (pointcount == 1) {
                        ej.setLastdisplacementvalue1(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLastdisplacementvalue2(0.0f);
                        ej.setLastdisplacementvalue3(0.0f);
                        ej.setLastdisplacementvalue4(0.0f);
                        ej.setLastdisplacementvalue5(0.0f);
                        ej.setLastdisplacementvalue6(0.0f);
                        ej.setLastdisplacementvalue7(0.0f);
                        ej.setLastdisplacementvalue8(0.0f);
                        ej.setLastdisplacementvalue9(0.0f);
                        ej.setLastdisplacementvalue10(0.0f);
                        setText("1#位移值 " + ej.getLastdisplacementvalue1());
                    } else if (pointcount == 2) {
                        ej.setLastdisplacementvalue1(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLastdisplacementvalue2(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLastdisplacementvalue3(0.0f);
                        ej.setLastdisplacementvalue4(0.0f);
                        ej.setLastdisplacementvalue5(0.0f);
                        ej.setLastdisplacementvalue6(0.0f);
                        ej.setLastdisplacementvalue7(0.0f);
                        ej.setLastdisplacementvalue8(0.0f);
                        ej.setLastdisplacementvalue9(0.0f);
                        ej.setLastdisplacementvalue10(0.0f);
                        setText("1#位移值 " + ej.getLastdisplacementvalue1());
                        setText("2#位移值 " + ej.getLastdisplacementvalue2());
                    } else if (pointcount == 3) {
                        ej.setLastdisplacementvalue3(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("3#位移值 " + ej.getLastdisplacementvalue3());
                    } else if (pointcount == 4) {
                        ej.setLastdisplacementvalue4(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("4#位移值 " + ej.getLastdisplacementvalue4());
                    } else if (pointcount == 5) {
                        ej.setLastdisplacementvalue5(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("5#位移值 " + ej.getLastdisplacementvalue5());
                    } else if (pointcount == 6) {
                        ej.setLastdisplacementvalue6(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("6#位移值 " + ej.getLastdisplacementvalue6());
                    } else if (pointcount == 7) {
                        ej.setLastdisplacementvalue7(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("7#位移值 " + ej.getLastdisplacementvalue7());
                    } else if (pointcount == 8) {
                        ej.setLastdisplacementvalue8(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("8#位移值 " + ej.getLastdisplacementvalue8());
                    } else if (pointcount == 9) {
                        ej.setLastdisplacementvalue9(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("9#位移值 " + ej.getLastdisplacementvalue9());
                    } else if (pointcount == 10) {
                        ej.setLastdisplacementvalue10(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        setText("10#位移值 " + ej.getLastdisplacementvalue10());

                    }
                    // currcmd ++;

                    AddAndUpdate1(record, ej);
                } else if (addrint >= 2) {
                    startindex = 6;
                    // lck.lock();
                    if (addrint == 2) {
                        ej.setLasthumidityvalue1(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue1(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasthumidityvalue2(0.0f);
                        ej.setLasttemperaturevalue2(0.0f);
                        ej.setLasthumidityvalue3(0.0f);
                        ej.setLasttemperaturevalue3(0.0f);
                        ej.setLasthumidityvalue4(0.0f);
                        ej.setLasttemperaturevalue4(0.0f);
                        ej.setLasthumidityvalue5(0.0f);
                        ej.setLasttemperaturevalue5(0.0f);
                        ej.setLasthumidityvalue6(0.0f);
                        ej.setLasttemperaturevalue6(0.0f);
                        ej.setLasthumidityvalue7(0.0f);
                        ej.setLasttemperaturevalue7(0.0f);
                        ej.setLasthumidityvalue8(0.0f);
                        ej.setLasttemperaturevalue8(0.0f);
                        ej.setLasthumidityvalue9(0.0f);
                        ej.setLasttemperaturevalue9(0.0f);
                        ej.setLasthumidityvalue10(0.0f);
                        ej.setLasttemperaturevalue10(0.0f);
                        setText("1#温度值 " + ej.getLasttemperaturevalue1());
                        setText("1#湿度值 " + ej.getLasthumidityvalue1());
                    } else if (addrint == 3) {

                        ej.setLasthumidityvalue1(0.0f);
                        ej.setLasttemperaturevalue1(0.0f);
                        ej.setLasthumidityvalue2(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue2(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasthumidityvalue3(0.0f);
                        ej.setLasttemperaturevalue3(0.0f);
                        ej.setLasthumidityvalue4(0.0f);
                        ej.setLasttemperaturevalue4(0.0f);
                        ej.setLasthumidityvalue5(0.0f);
                        ej.setLasttemperaturevalue5(0.0f);
                        ej.setLasthumidityvalue6(0.0f);
                        ej.setLasttemperaturevalue6(0.0f);
                        ej.setLasthumidityvalue7(0.0f);
                        ej.setLasttemperaturevalue7(0.0f);
                        ej.setLasthumidityvalue8(0.0f);
                        ej.setLasttemperaturevalue8(0.0f);
                        ej.setLasthumidityvalue9(0.0f);
                        ej.setLasttemperaturevalue9(0.0f);
                        ej.setLasthumidityvalue10(0.0f);
                        ej.setLasttemperaturevalue10(0.0f);

                        setText("2#温度值 " + ej.getLasttemperaturevalue2());
                        setText("2#湿度值 " + ej.getLasthumidityvalue2());
                    } else if (addrint == 4) {
                        ej.setLasthumidityvalue3(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue3(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("3#温度值 " + ej.getLasttemperaturevalue3());
                        setText("3#湿度值 " + ej.getLasthumidityvalue3());
                    } else if (addrint == 5) {
                        ej.setLasthumidityvalue4(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue4(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("4#温度值 " + ej.getLasttemperaturevalue4());
                        setText("4#湿度值 " + ej.getLasthumidityvalue4());
                    } else if (addrint == 6) {
                        ej.setLasthumidityvalue5(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue5(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("5#温度值 " + ej.getLasttemperaturevalue5());
                        setText("5#湿度值 " + ej.getLasthumidityvalue5());
                    } else if (addrint == 7) {
                        ej.setLasthumidityvalue6(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue6(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("6#温度值 " + ej.getLasttemperaturevalue6());
                        setText("6#湿度值 " + ej.getLasthumidityvalue6());
                    } else if (addrint == 8) {
                        ej.setLasthumidityvalue7(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue7(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("7#温度值 " + ej.getLasttemperaturevalue7());
                        setText("7#湿度值 " + ej.getLasthumidityvalue7());
                    } else if (addrint == 9) {
                        ej.setLasthumidityvalue8(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue8(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("8#温度值 " + ej.getLasttemperaturevalue8());
                        setText("8#湿度值 " + ej.getLasthumidityvalue8());
                    } else if (addrint == 10) {
                        ej.setLasthumidityvalue9(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue9(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("9#温度值 " + ej.getLasttemperaturevalue9());
                        setText("9#湿度值 " + ej.getLasthumidityvalue9());
                    } else if (addrint == 11) {
                        ej.setLasthumidityvalue10(parseHex88(str.substring(startindex, startindex += 4)) / 10);
                        ej.setLasttemperaturevalue10(parseHex88(str.substring(startindex, startindex += 4)) / 10);

                        setText("10#温度值 " + ej.getLasttemperaturevalue10());
                        setText("10#湿度值 " + ej.getLasthumidityvalue10());

                    }
                    // currcmd ++;
                    // lck.unlock();
                    AddAndUpdate2(record, ej);

                    // setText("内部电压1：" + ej.getLastinternalvoltage1()+"V");
                    // setText("内部电压2：" + ej.getLastinternalvoltage2()+"V");
                    // setText("内部电压3：" + ej.getLastinternalvoltage3()+"V");
                    // setText("内部电压4：" + ej.getLastinternalvoltage4()+"V");
                    // setText("内部电压5：" + ej.getLastinternalvoltage5()+"V");
                    // setText("装置温度 " + ej.getLasttemperature ());

                }

                break;
            case 4:
                startindex = 6;
                // ej.setLastinternalvoltage1
                // (IEEE754Utils.strbytesToSingleTurn(str.substring(startindex,
                // startindex+=8)).floatValue());
                // ej.setLastinternalvoltage2
                // (IEEE754Utils.strbytesToSingleTurn(str.substring(startindex,
                // startindex+=8)).floatValue());
                // ej.setLastinternalvoltage3
                // (IEEE754Utils.strbytesToSingleTurn(str.substring(startindex,
                // startindex+=8)).floatValue());
                // ej.setLastinternalvoltage4
                // (IEEE754Utils.strbytesToSingleTurn(str.substring(startindex,
                // startindex+=8)).floatValue());
                // ej.setLastinternalvoltage5
                // (IEEE754Utils.strbytesToSingleTurn(str.substring(startindex,
                // startindex+=8)).floatValue());
                startindex += 8;
                startindex += 8;
                startindex += 8;
                startindex += 8;
                startindex += 8;
                ej.setLasttemperature(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue1(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue2(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue3(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue4(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue5(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue6(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue7(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastdisplacementvalue8(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue1(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue1(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue2(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue2(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue3(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue3(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue4(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue4(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue5(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue5(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue6(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue6(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue7(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue7(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasttemperaturevalue8(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLasthumidityvalue8(
                        IEEE754Utils.strbytesToSingleTurn(str.substring(startindex, startindex += 8)).floatValue());
                ej.setLastcommunicationstatus(0);
                ej.setLasthisdatatime(addHistory(ej));
                setText(ad.getName() + "设备返回数据： 站点号：1 监测点" + addrint);
                // setText("内部电压1：" + ej.getLastinternalvoltage1()+"V");
                // setText("内部电压2：" + ej.getLastinternalvoltage2()+"V");
                // setText("内部电压3：" + ej.getLastinternalvoltage3()+"V");
                // setText("内部电压4：" + ej.getLastinternalvoltage4()+"V");
                // setText("内部电压5：" + ej.getLastinternalvoltage5()+"V");
                setText("装置温度 " + ej.getLasttemperature());
                setText("1#位移值 " + ej.getLastdisplacementvalue1());
                setText("2#位移值 " + ej.getLastdisplacementvalue2());
                setText("3#位移值 " + ej.getLastdisplacementvalue3());
                setText("4#位移值 " + ej.getLastdisplacementvalue4());
                setText("5#位移值 " + ej.getLastdisplacementvalue5());
                setText("6#位移值 " + ej.getLastdisplacementvalue6());
                setText("7#位移值 " + ej.getLastdisplacementvalue7());
                setText("8#位移值 " + ej.getLastdisplacementvalue8());
                setText("1#温度值 " + ej.getLasttemperaturevalue1());
                setText("1#湿度值 " + ej.getLasthumidityvalue1());
                setText("2#温度值 " + ej.getLasttemperaturevalue2());
                setText("2#湿度值 " + ej.getLasthumidityvalue2());
                setText("3#温度值 " + ej.getLasttemperaturevalue3());
                setText("3#湿度值 " + ej.getLasthumidityvalue3());
                setText("4#温度值 " + ej.getLasttemperaturevalue4());
                setText("4#湿度值 " + ej.getLasthumidityvalue4());
                setText("5#温度值 " + ej.getLasttemperaturevalue5());
                setText("5#湿度值 " + ej.getLasthumidityvalue5());
                setText("6#温度值 " + ej.getLasttemperaturevalue6());
                setText("6#湿度值 " + ej.getLasthumidityvalue6());
                setText("7#温度值 " + ej.getLasttemperaturevalue7());
                setText("7#湿度值 " + ej.getLasthumidityvalue7());
                setText("8#温度值 " + ej.getLasttemperaturevalue8());
                setText("8#湿度值 " + ej.getLasthumidityvalue8());
                break;
            case 10:
                setText("写入设备指令成功");
                break;
            default:
                break;
        }
        // Ejserviceimpl.Update(ej, false);

    }

    @Transactional
    private void AddAndUpdate1(Ejsensor record, Ejsensor sensor) {
        alarmLevel = 0;// 报警等级
        alarmMessage = "";// 报警消息
        // 报警逻辑
        // 这里写报警逻辑
        if (record.getdisplacementalarmvalue() == null ||
                record.getdisplacementalarmrelievevalue() == null ||
                record.getdisplacementwarnvalue() == null ||
                record.getdisplacementwarnrelievevalue() == null) {
            setText("设备地址" + record.getDeviceaddr() + " 未设置报警值！");
            return;
        }
        array.clear();
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue1()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue2()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue3()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue4()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue5()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue6()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue7()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue8()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue9()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());
        sensor.setLastdisplacementalarm(
                GetAlarmStatus(isnull(sensor.getLastdisplacementvalue10()), isnull(record.getdisplacementalarmvalue()),
                        isnull(record.getdisplacementalarmrelievevalue()), isnull(record.getdisplacementwarnvalue()),
                        isnull(record.getdisplacementwarnrelievevalue()),
                        isnull(sensor.getLastdisplacementalarm()), true, "位移", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLastdisplacementalarm(), "位移");
        array.add(sensor.getLastdisplacementalarm());

        // String alm = "";
        String cstr = "";
        int displacementalm = 0;
        int changealm = record.getLastdisplacementalarm();
        // int changealm=0;
        // record.setLastalarmlevel(0);
        // record.setLastalarmmessage("");
        if (isnull(sensor.getLastdisplacementvalue1()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue2()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue3()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue4()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue5()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue6()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue7()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue8()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue9()) >= record.getdisplacementalarmvalue() ||
                isnull(sensor.getLastdisplacementvalue10()) >= record.getdisplacementalarmvalue()) {
            // alm+="位移告警,";
            sensor.setLastdisplacementalarm(1);
            // sensor.setLastalarmlevel(2);
            // sensor.setLastalarmmessage(alm);
            displacementalm = 1;

            // String cstr = "";
            cstr = String.format("%02X101000%04X", addrindex, displacementalm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();
            // setText("向" + "设备发送指令" + cstr);
        } else {
            sensor.setLastdisplacementalarm(0);
            // sensor.setLastalarmlevel(0);
            // sensor.setLastalarmmessage(alm);
            displacementalm = 0;

            // String cstr = "";
            cstr = String.format("%02X101000%04X", addrindex, displacementalm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();
        }
        if (changealm != displacementalm) {
            lck.lock();
            // Thread.sleep(1000);
            Send(cstr);
            lck.unlock();
        }

        alarmLevel = HexUtil.MAX(array);

        // sensor.setLastalarmlevel(alarmLevel);
        // sensor.setLastalarmmessage(alarmMessage);
        sensor.setLastcommunicationstatus(0);
        sensor.setLastdatatime(new Date());
        sensor.setLasthisdatatime(addHistory(record));
        if (Ejserviceimpl.Update(sensor, false) == 0) {
            setText("更新实时数据失败，请检查数据库连接");
        }
    }

    @Transactional
    private void AddAndUpdate2(Ejsensor record, Ejsensor sensor) {
        // int alarmLevel = 0;// 报警等级
        // String alarmMessage = "";// 报警消息
        // 报警逻辑
        // 这里写报警逻辑
        if (
        // record.getdisplacementalarmvalue() == null ||
        // record.getdisplacementalarmrelievevalue() == null ||
        // record.getdisplacementwarnvalue() == null ||
        // record.getdisplacementwarnrelievevalue() == null ||
        record.gettemperaturealarmvalue() == null ||
                record.gettemperaturealarmrelievevalue() == null ||
                record.gettemperaturewarnvalue() == null ||
                record.gettemperaturewarnrelievevalue() == null ||
                record.gethumidityalarmvalue() == null ||
                record.gethumidityalarmrelievevalue() == null ||
                record.gethumiditywarnvalue() == null ||
                record.gethumiditywarnrelievevalue() == null) {
            setText("设备地址" + record.getDeviceaddr() + " 未设置报警值！");
            return;
        }
        String alm = "";
        int displacementalm = 0, temperaturealm = 0, humidityalm = 0;
        // sensor.setLastalarmlevel(0);
        // sensor.setLastalarmmessage("");
        // List<Integer> array = new ArrayList<>();
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue1()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue2()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue3()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue4()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue5()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue6()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue7()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue8()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue9()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        sensor.setLasttemperaturealarm(
                GetAlarmStatus(isnull(sensor.getLasttemperaturevalue10()), isnull(record.gettemperaturealarmvalue()),
                        isnull(record.gettemperaturealarmrelievevalue()), isnull(record.gettemperaturewarnvalue()),
                        isnull(record.gettemperaturewarnrelievevalue()),
                        isnull(sensor.getLasttemperaturealarm()), true, "温度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasttemperaturealarm(), "温度");
        array.add(sensor.getLasttemperaturealarm());
        // String alm = "";
        String cstr = "";
        // int temperaturealm=0;
        int changealm = record.getLasttemperaturealarm();
        if (isnull(sensor.getLasttemperaturevalue1()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue2()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue3()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue4()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue5()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue6()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue7()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue8()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue9()) >= record.gettemperaturealarmvalue() ||
                isnull(sensor.getLasttemperaturevalue10()) >= record.gettemperaturealarmvalue()) {
            // alm+="温度告警,";
            sensor.setLasttemperaturealarm(1);
            // sensor.setLastalarmlevel(2);
            // sensor.setLastalarmmessage(alm);
            temperaturealm = 1;
            // String cstr = "";
            cstr = String.format("%02X101001%04X", addrindex, temperaturealm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();

            // setText("向" + "设备发送指令" + cstr);
        } else {
            sensor.setLasttemperaturealarm(0);
            // sensor.setLastalarmlevel(0);
            // sensor.setLastalarmmessage(alm);
            // temperaturealm = 0;

            // String cstr = "";
            cstr = String.format("%02X101001%04X", addrindex, temperaturealm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();
        }
        if (changealm != temperaturealm) {
            lck.lock();
            // Thread.sleep(1000);
            Send(cstr);
            lck.unlock();
        }

        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue1()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue2()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue3()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue4()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue5()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue6()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue7()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue8()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue9()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());
        sensor.setLasthumidityalarm(
                GetAlarmStatus(isnull(sensor.getLasthumidityvalue10()), isnull(record.gethumidityalarmvalue()),
                        isnull(record.gethumidityalarmrelievevalue()), isnull(record.gethumiditywarnvalue()),
                        isnull(record.gethumiditywarnrelievevalue()),
                        isnull(sensor.getLasthumidityalarm()), true, "湿度", record.getDevicegroup(),
                        record.getDeviceaddr(),
                        record.getPointname()));
        alarmMessage += GetAlarmMessage(sensor.getLasthumidityalarm(), "湿度");
        array.add(sensor.getLasthumidityalarm());

        changealm = record.getLasthumidityalarm();
        if (isnull(sensor.getLasthumidityvalue1()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue2()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue3()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue4()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue5()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue6()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue7()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue8()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue9()) >= record.gethumidityalarmvalue() ||
                isnull(sensor.getLasthumidityvalue10()) >= record.gethumidityalarmvalue()) {
            // alm+="湿度告警,";
            sensor.setLasthumidityalarm(1);
            // sensor.setLastalarmlevel(2);
            // sensor.setLastalarmmessage(alm);
            humidityalm = 1;

            // String cstr = "";
            cstr = String.format("%02X101002%04X", addrindex, humidityalm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();
            // setText("向" + "设备发送指令" + cstr);
        } else {
            sensor.setLasthumidityalarm(0);
            // sensor.setLastalarmlevel(0);
            // sensor.setLastalarmmessage(alm);
            humidityalm = 0;

            // String cstr = "";
            cstr = String.format("%02X101002%04X", addrindex, humidityalm);
            cstr = cstr + CRC16.calcCrc16(cstr);
            // lck.lock();
            // Send(cstr);
            // lck.unlock();
        }
        if (changealm != humidityalm) {
            lck.lock();
            // Thread.sleep(1000);
            Send(cstr);
            lck.unlock();
        }
        // String cstr = "";
        alarmLevel = HexUtil.MAX(array);
        sensor.setLastalarmlevel(alarmLevel);
        sensor.setLastalarmmessage(alarmMessage);
        sensor.setLastcommunicationstatus(0);
        sensor.setLastdatatime(new Date());
        sensor.setLasthisdatatime(addHistory(record));
        if (Ejserviceimpl.Update(sensor, false) == 0) {
            setText("更新实时数据失败，请检查数据库连接");
        }
    }

    private Date addHistory(Ejsensor sensor) {
        // sensor = Ejserviceimpl.getModel(sensor);
        if (sensor != null) {
            if (sensor.getLasthisdatatime() == null) {
                add(sensor);
                return new Date();
            } else {
                long lasttime = 0;
                long nowtime = 0;
                try {
                    lasttime = HexUtil.todate(sensor.getLasthisdatatime()).getTime();
                    nowtime = HexUtil.todate(new Date()).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int timersp = 60000;
                if (sensor.getLastalarmlevel() != 0) {
                    timersp = ad.getHisalarmtimersp() * 1000;
                } else {
                    timersp = ad.getHistimersp() * 1000;
                }
                if (lasttime + timersp < nowtime) {
                    add(sensor);
                    return new Date();
                }
            }
        }
        return null;
    }

    private void add(Ejsensor sensor) {
        Ejhistory his = new Ejhistory();
        his.setStationid(sensor.getStationid());
        his.setDevicegroup(sensor.getDevicegroup());
        his.setDeviceaddr(sensor.getDeviceaddr());
        his.setDatatime(sensor.getLastdatatime());
        his.setPointname(sensor.getPointname());
        his.setInternalvoltage1(sensor.getLastinternalvoltage1());
        his.setInternalvoltage2(sensor.getLastinternalvoltage2());
        his.setInternalvoltage3(sensor.getLastinternalvoltage3());
        his.setInternalvoltage4(sensor.getLastinternalvoltage4());
        his.setInternalvoltage5(sensor.getLastinternalvoltage5());
        his.setTemperature(sensor.getLasttemperature());
        his.setDisplacementvalue1(sensor.getLastdisplacementvalue1());
        his.setDisplacementvalue2(sensor.getLastdisplacementvalue2());
        his.setDisplacementvalue3(sensor.getLastdisplacementvalue3());
        his.setDisplacementvalue4(sensor.getLastdisplacementvalue4());
        his.setDisplacementvalue5(sensor.getLastdisplacementvalue5());
        his.setDisplacementvalue6(sensor.getLastdisplacementvalue6());
        his.setDisplacementvalue7(sensor.getLastdisplacementvalue7());
        his.setDisplacementvalue8(sensor.getLastdisplacementvalue8());
        his.setDisplacementvalue9(sensor.getLastdisplacementvalue9());
        his.setDisplacementvalue10(sensor.getLastdisplacementvalue10());
        his.setTemperaturevalue1(sensor.getLasttemperaturevalue1());
        his.setHumidityvalue1(sensor.getLasthumidityvalue1());
        his.setTemperaturevalue2(sensor.getLasttemperaturevalue2());
        his.setHumidityvalue2(sensor.getLasthumidityvalue2());
        his.setTemperaturevalue3(sensor.getLasttemperaturevalue3());
        his.setHumidityvalue3(sensor.getLasthumidityvalue3());
        his.setTemperaturevalue4(sensor.getLasttemperaturevalue4());
        his.setHumidityvalue4(sensor.getLasthumidityvalue4());
        his.setTemperaturevalue5(sensor.getLasttemperaturevalue5());
        his.setHumidityvalue5(sensor.getLasthumidityvalue5());
        his.setTemperaturevalue6(sensor.getLasttemperaturevalue6());
        his.setHumidityvalue6(sensor.getLasthumidityvalue6());
        his.setTemperaturevalue7(sensor.getLasttemperaturevalue7());
        his.setHumidityvalue7(sensor.getLasthumidityvalue7());
        his.setTemperaturevalue8(sensor.getLasttemperaturevalue8());
        his.setHumidityvalue8(sensor.getLasthumidityvalue8());
        his.setTemperaturevalue9(sensor.getLasttemperaturevalue9());
        his.setHumidityvalue9(sensor.getLasthumidityvalue9());
        his.setTemperaturevalue10(sensor.getLasttemperaturevalue10());
        his.setHumidityvalue10(sensor.getLasthumidityvalue10());
        his.setDevicefaultsignal(sensor.getLastdevicefaultsignal());
        his.setTimingsignalstatus(sensor.getLasttimingsignalstatus());
        his.setTimeservicestatus(sensor.getLasttimeservicestatus());
        his.setTimehoppingframemeasurementstatus(sensor.getLasttimehoppingframemeasurementstatus());
        his.setInput1(sensor.getLastinput1());
        his.setInput2(sensor.getLastinput2());
        his.setInput3(sensor.getLastinput3());
        his.setInput4(sensor.getLastinput4());
        his.setInput5(sensor.getLastinput5());
        his.setInput6(sensor.getLastinput6());
        his.setInput7(sensor.getLastinput7());
        his.setInput8(sensor.getLastinput8());
        his.setOutlet1status(sensor.getLastoutlet1status());
        his.setOutlet2status(sensor.getLastoutlet2status());
        his.setOutlet3status(sensor.getLastoutlet3status());
        his.setOutlet4status(sensor.getLastoutlet4status());
        his.setOutlet5status(sensor.getLastoutlet5status());
        his.setOutlet6status(sensor.getLastoutlet6status());
        his.setOutlet7status(sensor.getLastoutlet7status());
        his.setOutlet8status(sensor.getLastoutlet8status());
        his.setAlarmstatus(sensor.getLastalarmstatus());
        his.setDeploymentstatus(sensor.getLastdeploymentstatus());
        his.setAlarmlevel(sensor.getLastalarmlevel());
        his.setAlarmmessage(sensor.getLastalarmmessage());
        Ejserviceimpl.Add(his);
    }

    @Async("threadPool")
    public void startSendThread(Appdevice Appdevice) throws Exception {
        String cmd[] = new String[3];
        cmd[0] = "%s0200000020";// 遥信
        cmd[1] = "%s041000003C";// 遥测
        checkType = "crc16";
        issend.put(Thread.currentThread().getId(), true);
        String cstr = "";
        pointcount = Appdevice.getPointcount();

        int timersp = (int) Math.round(Appdevice.getTimersp() * 1000);
        int timeroutsp = (int) Math.round(Appdevice.getTimeoutsp() * 1000);
        while (issend.get(Thread.currentThread().getId()) != null) {
            if (ad.getBrand().equals("外购")) {
                // synchronized (lock) {
                if (isrun) {
                    if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
                        addrindex = Appdevice.getAddrmin();

                    cstr = String.format(cmd[currcmd], String.format("%02X", addrindex));
                    cstr = cstr + CRC16.calcCrc16(cstr);
                    Send(cstr);
                    // lock.wait(timersp);
                    currcmd++;

                    cstr = String.format(cmd[currcmd], String.format("%02X", addrindex));
                    cstr = cstr + CRC16.calcCrc16(cstr);
                    Send(cstr);
                    // lock.wait(timersp);
                    currcmd = 0;

                    reSend[addrindex]++;
                    if (reSend[addrindex] > 3) {
                        Ejserviceimpl.UpdateStatus(ad, addrindex, 1);
                        reSend[addrindex] = 0;
                        // addrindex++;
                    }
                    if (!controllerCmd.equals("")) {
                        Send(cstr);
                        // lock.wait(timersp);
                        controllerCmd = "";
                    }
                } else if (rescode != 0) {
                    setText(resmsg[rescode]);
                }
                // }
            } else {
                if (isAvailable()) {
                    if (isrun) {

                        if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
                            addrindex = Appdevice.getAddrmin();

                        currcmd++;

                        if (currcmd > pointcount + 1)
                            currcmd = 1;
                        switch (currcmd) {
                            case 1:
                                cstr = "0103" + "00b0" + String.format("%04X", pointcount);
                                // cstr = String.format("%02X", addrindex) + "0300b00001";
                                cstr = cstr + CRC16.calcCrc16(cstr);
                                lck.lock();
                                // if(recvflag==1)
                                Send(cstr);
                                lck.unlock();
                                // recvflag = 0;
                                // lock.wait(timersp);
                                // setText("向" + Appdevice.getName() + "设备发送指令" + cstr);
                                reSend[addrindex]++;
                                if (reSend[addrindex] > 3) {
                                    Ejserviceimpl.UpdateStatus(ad, addrindex, 1);
                                    reSend[addrindex] = 0;
                                    // recvflag = 1;

                                }
                                break;
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                            case 6:
                            case 7:
                            case 8:
                            case 9:
                            case 10:
                            case 11:
                                cstr = String.format("%02X", currcmd) + "03" + "0000" + "0002";
                                cstr = cstr + CRC16.calcCrc16(cstr);
                                lck.lock();
                                // if(recvflag==1)
                                Send(cstr);
                                lck.unlock();
                                // recvflag = 0;
                                reSend[addrindex]++;
                                if (reSend[addrindex] > 3) {
                                    // Ejserviceimpl.UpdateStatus(ad, addrindex,1);
                                    reSend[addrindex] = 0;
                                    // recvflag = 1;

                                }
                                break;

                            // case 11:

                            // cstr = String.format("%02X", currcmd-9) + "03" +"0000" +"0002";
                            // // cstr = String.format("%02X", addrindex) + "0300000002";
                            // cstr = cstr + CRC16.calcCrc16(cstr);
                            // try {
                            // lck.lock();
                            // if(recvflag==1)
                            // Send(cstr);
                            // lck.unlock();
                            // recvflag = 0;
                            // //lock.wait(timersp);
                            // } catch (Exception EX) {
                            // setText(EX.toString());
                            // }
                            // reSend[addrindex]++;
                            // if (reSend[addrindex] > 3) {
                            // Ejserviceimpl.UpdateStatus(ad, addrindex,1);
                            // reSend[addrindex] = 0;
                            // recvflag = 1;
                            // currcmd ++;
                            // }
                            // break;
                            // case 12:
                            // case 13:
                            // case 14:
                            // case 15:
                            // case 16:
                            // case 17:
                            // case 18:
                            // case 19:
                            // case 20:
                            // case 21:

                            // cstr = String.format("%02X", currcmd-9) + "03" +"0000" +"0002";
                            // // cstr = String.format("%02X", addrindex) + "0300000002";
                            // cstr = cstr + CRC16.calcCrc16(cstr);
                            // try {
                            // lck.lock();
                            // if(recvflag==1)
                            // Send(cstr);
                            // lck.unlock();
                            // recvflag = 0;
                            // //lock.wait(timersp);
                            // } catch (Exception EX) {
                            // setText(EX.toString());
                            // }

                            // reSend[addrindex]++;
                            // if (reSend[addrindex] > 3) {
                            // //Ejserviceimpl.UpdateStatus(ad, addrindex,1);
                            // reSend[addrindex] = 0;
                            // recvflag = 1;
                            // currcmd ++;
                            // }
                            // break;
                        }

                        // }
                    } else if (rescode != 0) {
                        setText(resmsg[rescode]);
                        // currcmd=1;

                    }
                }
            }
            Thread.sleep(timersp);
        }
    }

    public void CreateCommand(Appdevice record, String... cmd) {
        String cstr = "";
        cstr = record.getDeviceaddr() + "05000" + cmd[0] + "FF00";
        cstr = cstr + CRC16.calcCrc16(cstr);
        controllerCmd = cstr;
    }

    public static float parseHex88(String num) {
        int ret = Integer.parseInt(num, 16);
        if (num.length() == 2) {
            return ret;
        } else if (num.length() == 4) {
            ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
            return ret;
        } else {
            throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");
        }
    }

    public static float parseHex88L(String num) {
        String sl = num.substring(0, 2);
        String sh = num.substring(2, 4);
        int ret = Integer.parseInt(sl, 16) + Integer.parseInt(sh, 16) * 0xff;
        if (num.length() == 2) {
            return ret;
        } else if (num.length() == 4) {
            ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
            return ret;
        } else {
            throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");
        }
    }
}
